// order.js
var app = getApp()
import { formatData } from "../../utils/util.js";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    passVule: {
      title:'指派人',
      name:'zhangaj',
      type:'安装单'
    },
    page:10,
    stateNum:null,
    orderList:[],
    order_type:null,
    activeIndex:''
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let self=this
    self.installList()
    self.orderStateNum()
  },
  //获取安装单
  installList:function(){
    let self = this
    app.query("web/orders/assign_orders", self.data.page).then(res=>{
      if(res.code===200){
        let formatOrders = formatData(res.orders)
        self.setData({
          orderList: formatOrders,
        })
        console.log(res)
      }
    })
  },
  //接单
  orderButton:function(event){
    let self = this
    let dataset = event.target.dataset
    app.query("web/orders/" + dataset.id + " /confirm_assign_order", "", "post").then(res=>{
      if(res.code===200){
        console.log(res)
        self.data.orderList.splice(dataset.index, 1);
        self.setData({
          orderList: self.data.orderList
        });
      }
    })
  },
  //订单详情
  orderDetail:function(e){
    let self = this
    self.setData({
      order_type: e.currentTarget.dataset.abnormalid
    })
    console.log(self.data.order_type)
    if (self.data.order_type!=null){
      wx.navigateTo({ url: '/pages/orderDetail/orderDetail?order_type=' + self.data.order_type})
    }
  },
  //异常订单详情
  orderAbnomal:function(e){
     let self = this
     console.log(e)
     let num=e.currentTarget.dataset.num
     if(num!=0){
       self.setData({
          order_type: e.currentTarget.dataset.abnormalid
       })
       wx.navigateTo({ url: '/pages/orderAbnormal/orderAbnormal?order_type=' + self.data.order_type})
     }
  },
  //获取订单各状态下的数
  orderStateNum:function(){
    let self=this
    app.query("web/orders/pending_orders").then(res=>{
      if(res.code===200){
        self.setData({
          stateNum: res.options
        })
        console.log(self.data.stateNum);
      }
    })
  },
})